<template>
	<view>
		<!-- bindgetphonenumber -->
		<button type="primary" open-type="getUserInfo" @click="getMyUserInfo" withCredentials="true">微信登录</button>
		<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取电话号码</button>
		<button @click="showGetPhoneNumber">绑定电话提示框</button>
		<wl-modal ref="modal"  :cancelFunction="getPhoneNumber"></wl-modal>
	</view>
</template>

<script>
import wlModal from '@/components/wl-modal/wl-modal.vue'
export default {
	data() {
		return {
			globalData:{
				userInfo:'',
			}
		};
	},
	onLoad: function() {
		uni.login({
			success: function(res) {
				// 获取code
				// console.log(JSON.stringify(res));
			}
		});
	},
	onShow() {
		this.showGetPhoneNumber()
	},
	methods: {
		showGetPhoneNumber: function(e){
				this.$refs.modal.showModal({
					title: '授权登录', // 提示标题
					content: '您还未授权登陆，部分功能将不能使用，是否授权登录？', // 提示内容
					cancelText: "绑定手机", // 取消按钮的文字  
					confirmText: "手动绑定", // 确认按钮文字  
					showCancel: true, // 是否显示取消按钮，默认为 true
					confirmColor: '#3CC51F', // 确定按钮颜色
					cancelColor: '#c70109', // 取消按钮颜色
					textAlign: 'center' ,// 对齐方式
					confirmOpenType: '', // 确定按钮open-type
					cancelOpenType: 'getPhoneNumber', // 取消按钮open-type
					success: (res) => {
						console.log(res)
						if(res.confirm){
							uni.showToast({
								title: '点击了确定',
								icon: 'none',
								duration: 2000
							})
						}else{
							this.getPhoneNumber()
							uni.showToast({
								title: '点击了取消',
								icon: 'none',
								duration: 2000
							})
						}
					} // 回调方法
				})
			},
		getPhoneNumber: function(e) {// 方法通过绑定自定义属性方式传给子组件
			// this.visible = false;// 用于关闭对话框
			console.log(e);
			if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
				console.log('用户点击了拒绝');
			} else {
				console.log(JSON.stringify(e.detail.encryptedData));
				console.log(JSON.stringify(e.detail.iv));
				this.visible = false;// 用于关闭对话框
			}
			
		},
		getMyUserInfo: function(loginType, cb) {
			console.log(loginType)
			console.log(cd)
			var that = this;
			// if (this.globalData.userInfo) {
			// 	typeof cb == 'function' && cb(this.globalData.userInfo, true);
			// } else {
				//1.调用登录接口
				uni.login({
					success: function() {
						uni.getUserInfo({
							success: function(res) {
								that.globalData.userInfo = res.userInfo;
								typeof cb == 'function' && cb(that.globalData.userInfo, true);
							},
							fail: function() {
								//2.第一次登陆不强制授权，直接返回
								if (loginType == 0) {
									typeof cb == 'function' && cb(that.globalData.userInfo, false);
								} else {
									//3.授权友好提示
									uni.showModal({
										title: '提示',
										content: '您还未授权登陆，部分功能将不能使用，是否重新授权？',
										showCancel: true,
										cancelText: '否',
										confirmText: '是',
										success: function(res) {
											//4.确认授权调用wx.openSetting
											if (res.confirm) {
												if (uni.openSetting) {
													//当前微信的版本 ，是否支持openSetting
													uni.openSetting({
														success: res => {
															if (res.authSetting['scope.userInfo']) {
																//如果用户重新同意了授权登录
																uni.getUserInfo({
																	success: function(res) {
																		that.globalData.userInfo = res.userInfo;
																		typeof cb == 'function' && cb(that.globalData.userInfo, true);
																	}
																});
															} else {
																//用户还是拒绝
																typeof cb == 'function' && cb(that.globalData.userInfo, false);
															}
														},
														fail: function() {
															//调用失败，授权登录不成功
															typeof cb == 'function' && cb(that.globalData.userInfo, false);
														}
													});
												} else {
													typeof cb == 'function' && cb(that.globalData.userInfo, false);
												}
											} else {
												typeof cb == 'function' && cb(that.globalData.userInfo, false);
											}
										}
									});
								}
							}
						});
					}
				});
			// }
		}
		
	
	},
	components: {
		wlModal
	},
};
</script>

<style>
	
</style>
